// let babylon = require("babylon");
let Fs = require("fs");
let Path = require("path");
const vm = require("vm");

// let code = fs.readFileSync(path.join(__dirname,"code.js"),"utf-8");
// let ret =babylon.parse(code)
function async() {
    let project = Fs.readFileSync(Path.join(__dirname, 'project.js'), 'utf-8');
    // vm.runInThisContext(project);
    vm.runInThisContext(project);
    for (let i = 0; i < allScriptName.length; i++) {
        let scriptName = allScriptName[i];
        let arr = allScript[scriptName];
        if (arr === undefined || arr[0] === undefined) {
            console.log(`有问题的脚本: ${scriptName}`)
            debugger
            continue;
        }
        let func = arr[0];

        if (typeof func == "function") {
            // 剔除一些无用的代码
            let funcCode = func.toString();
            let replaceString = ['function (e, t, n)', ", cc._RF.pop();", "\"use strict\";", "cc._RF.push"]
            for (let j = 0; j < replaceString.length; j++) {
                let str = replaceString[j];
                if (funcCode.indexOf(str) !== -1) {
                    funcCode = funcCode.replace(str, "");
                }
            }

            // console.log(funcCode)

            let destFile = Path.join(__dirname, `../tuyoumajiang/code/${scriptName}.js`);
            if (Fs.existsSync(destFile)) {
                console.log("已经存在:" + scriptName);
            } else {
                Fs.writeFileSync(destFile, funcCode, 'utf-8');
            }
        } else {
            console.log(`不是function:${scriptName}`)
        }

    }
}


function dirAsync() {
    let dirArray = {};
    // 进行目录还原, hook require api
    global.require = function (url) {
        dirArray[url] = true;
    }
    global.cc = {
        Class() {
            return function () {

            }
        },
        info() {

        }
    };
    global.module = {
        exports: {},
    }
    global.t = {}
    global.tywx = {
        StateInfo: {},
        SystemInfo: {},
    }
    global.window = {
        Global: {}
    }
    let project = Fs.readFileSync(Path.join(__dirname, 'project.js'), 'utf-8');
    vm.runInThisContext(project);
    for (let i = 0; i < allScriptName.length; i++) {
        let script = allScriptName[i];
        let c1 = Fs.readFileSync(Path.join(__dirname, `../tuyoumajiang/code/${script}.js`), 'utf-8');
        console.log(script);
        vm.runInThisContext(c1);
    }

    console.log(dirArray)
    debugger
}

dirAsync();